package com.military.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
//@WebFilter(urlPatterns = "/*")//拦截所有请求
public class DemoFilter implements Filter {

    // 过滤器初始化, 在服务器启动时执行一次
    @Override

    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("初始化过滤器-----");
    }

    /**
     * 拦截到请求之后,执行,会执行多次
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("拦截到了请求------");
        // 放行,让请求继续往下走
        filterChain.doFilter(servletRequest,servletResponse);
    }

    /**
     * 过滤器销毁, 在服务器关闭时执行一次
     */
    @Override
    public void destroy() {
        log.info("销毁过滤器-----");
    }
}
